package cn.m3a1.control.admin;

import cn.m3a1.bean.Msg;
import cn.m3a1.bean.Tclass;
import cn.m3a1.bean.User;
import cn.m3a1.service.admin.TclassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wyj
 * @date 2019/7/3
 * @time 上午9:05
 */
@Controller
@RequestMapping("/admin")
public class AdminTclassController {
    @Autowired
    TclassService tclassService;

    @RequestMapping("/tclasshome")
    public String tclasshome(ModelMap modelMap) {
        List<Tclass> tclasses = tclassService.getTclass();
        List<User> users = tclassService.getMang();
        //  PageInfo pageInfo=new PageInfo();
        modelMap.put("members", tclasses);
        modelMap.put("users", users);
        return "Admin/AdminTclass";
    }

    @RequestMapping("/findstu")
    public String finstu(ModelMap modelMap,Integer id) {
        System.out.println("执行了。。。。。。");
        List<User> findstu = tclassService.findstu(id);
        String className = tclassService.getClassName(id);
        List<Tclass> tclasses = tclassService.getTclass();
        modelMap.put("classname",className);
        modelMap.put("users", findstu);
        modelMap.put("tclasses", tclasses);
        return "Admin/adminstudent";
    }


    @ResponseBody
    @RequestMapping("/addtclass")
    public Msg addtclass(Tclass tclass) {
        Boolean b = tclassService.add(tclass);
        if (b) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @ResponseBody
    @RequestMapping("/deltclass")
    public Msg deltclass(int id) {
        Boolean b = tclassService.deltclass(id);
        if (b) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @ResponseBody
    @RequestMapping("/upclass")
    public Msg upclass(Tclass tclass) {
        Boolean b = tclassService.uptclass(tclass);
        if (b) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @ResponseBody
    @RequestMapping("/getclass")
    public Msg getclass(int id) {
        Tclass tclass = tclassService.getTclass(id);
       return Msg.success().add("tclass", tclass);
    }

    /**
     * 从班级里删除学生
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delclassfromstu")
    public Msg delclassfromstu(int id) {
        Boolean b = tclassService.deltStuClass(id);
        if (b) {
            return Msg.success();
        }
        return Msg.fail();
    }
}